package com.ajoclass.demo.order.web;

import com.ajoclass.demo.order.entity.Order;
import com.ajoclass.demo.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/findByOrderSn",method = RequestMethod.POST)
    public Order findByOrderSn(@RequestBody Order o){
        return orderService.findByOrderSn("axss99384");
    }

    @RequestMapping(value = "/saveOrUpdate",method = RequestMethod.POST)
    public Order saveOrUpdate(@RequestBody Order o){
        if(StringUtils.isEmpty(o.getOrderId())){
            o.setOrderId(System.currentTimeMillis());
        }
        o.setCreateTime(new Date());
        o.setOrderSn("Oxx8394832");
        o.setUserId(3);
        return orderService.save(o);
    }

    @RequestMapping(value = "/paged",method = RequestMethod.POST)
    public Page<Order> paged(@RequestBody Order o){
        return orderService.findAll(o.getPageIndex(),o.getPageSize());
    }
}
